package cos.android.platform;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

public class PlatformView extends SurfaceView implements SurfaceHolder.Callback {

	private Context mContext;

	private PlatformThread platformThread;
	
	private TextView lifeView;
	
	

	public PlatformView(Context context, AttributeSet attrs) {
		super(context, attrs);

		SurfaceHolder holder = getHolder();
		holder.addCallback(this);

		platformThread = new PlatformThread(holder, context, new Handler(){
		    
            public void handleMessage(Message m) {

            	String life = Integer.toString(m.getData().getInt("life"));
            	lifeView.setText(life);

            }//end handle msg
        });
		
		setFocusable(true); // make sure we get key events

	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int width, int height) {
		platformThread.setSurfaceSize(width, height);

	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		platformThread.setRunning(true);

		platformThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		platformThread.setRunning(false);

		while (retry) {
			try {
				platformThread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}
	}


	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return platformThread.doTouchEvent(event);
	}
	

	

	public PlatformThread getPlatformThread() {
		return platformThread;
	}

	public void setPlatformThread(PlatformThread platformThread) {
		this.platformThread = platformThread;
	}

	public TextView getLifeView() {
		return lifeView;
	}

	public void setLifeView(TextView lifeView) {
		this.lifeView = lifeView;
	}
	
	
	
}
